package demo.student;

import java.io.*;
import java.util.*;
import java.util.stream.Stream;


public class StudentTest {
    List<Student> studentList = new ArrayList<>();

    public static void main(String[] args) {
        StudentTest studentTest = new StudentTest();
////        生成60个学生对象
//        studentTest.fun1();
////        将各科成绩的第一名(并列第一取学号小的学生)的学生保存至a.txt文件中
//        studentTest.fun2();
////        将总成绩前10名的学生信息保存至b.txt文件中
//        studentTest.fun3();
////        将各科平均分保存至c.txt中
//        studentTest.fun4();
////        将各科不及格的学员信息保存至d.txt中
//        studentTest.fun5();

//        验证结果
        studentTest.fun();


    }



    private void fun(){
        try (ObjectInputStream aois = new ObjectInputStream(new FileInputStream("demo23\\src\\a.txt"));
             ObjectInputStream bois = new ObjectInputStream(new FileInputStream("demo23\\src\\b.txt"));
             ObjectInputStream cois = new ObjectInputStream(new FileInputStream("demo23\\src\\c.txt"));
             ObjectInputStream dois = new ObjectInputStream(new FileInputStream("demo23\\src\\d.txt"));
        ){
            List<Student> alist = (List<Student>) aois.readObject();
            alist.forEach(System.out::println);
            System.out.println();
            List<Student> blist = (List<Student>) bois.readObject();
            blist.forEach(System.out::println);
            System.out.println();
            AverageScore c = (AverageScore) cois.readObject();
            System.out.println(c);
            System.out.println();
            List<Student> dlist = (List<Student>) dois.readObject();
            dlist.forEach(System.out::println);



        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void fun1(){
        for (int i = 1; i <= 60; i++) {
            Student student = new Student(i,
                    (int) (Math.random() * 50) + 50,
                    (int) (Math.random() * 50) + 50,
                    (int) (Math.random() * 50) + 50);
            student.setScore(student.getYuwen()+student.getYingyum()+student.getShuxue());
            studentList.add(student);
        }
    }

    private void fun2(){
        Optional<Student> max = studentList.stream().max((o1, o2) -> {
            if (o1.getScore().equals(o2.getScore())) {
                return o2.getNum() - o1.getNum();
            } else
                return o1.getScore() - o2.getScore();
        });
        Student studentMax = max.get();


        Student yuwenMax = studentList.stream().max((o1, o2) -> {
            if (o1.getYuwen().equals(o2.getYuwen())){
                return o1.getNum() - o2.getNum();
            }
            return o1.getYuwen() - o2.getYuwen();

        }).get();

        Student yingyuMax = studentList.stream().max((o1, o2) -> {
            if (o1.getYingyum().equals(o2.getYingyum())){
                return o1.getNum() - o2.getNum();
            }
            return o1.getYingyum() - o2.getYingyum();

        }).get();

        Student shuxueMax = studentList.stream().max((o1, o2) -> {
            if (o1.getShuxue().equals(o2.getShuxue())){
                return o1.getNum() - o2.getNum();
            }
            return o1.getShuxue() - o2.getShuxue();

        }).get();

        List<Student> maxlist  = new ArrayList<>();
        maxlist.add(yuwenMax);
        maxlist.add(yingyuMax);
        maxlist.add(shuxueMax);

        try(
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("demo23\\src\\a.txt"))
        ) {
            oos.writeObject(maxlist);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void fun3() {
        List<Student> list = studentList.stream()
                .sorted(Comparator.comparingInt(Student::getScore))
                .skip(50).limit(10).toList();
        System.out.println();
        try(
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("demo23\\src\\b.txt"))
        ) {
            oos.writeObject(list);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void fun4() {
        int yuwenAverage= studentList.stream().mapToInt(value -> value.getYuwen()).sum() / 60;
        int yingyuAverage= studentList.stream().mapToInt(value -> value.getYingyum()).sum() / 60;
        int shuxueAverage= studentList.stream().mapToInt(value -> value.getShuxue()).sum() / 60;
        AverageScore averageScore = new AverageScore(yuwenAverage,yingyuAverage,shuxueAverage);
        try(
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("demo23\\src\\c.txt"))
        ) {
            oos.writeObject(averageScore);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }

    private void fun5() {
        List<Student> list = studentList.stream().map(item->{
            if (item.getYuwen() < 60 || item.getYingyum() < 60 || item.getShuxue() < 60){
                return item;
            }
            return null;
        }).filter(student -> {
            if (student == null)
                return false;
            return true;
        }).toList();
        try(
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("demo23\\src\\d.txt"))
        ) {
            oos.writeObject(list);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }
}
